package edu.bit.water.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import edu.bit.water.dao.EntityEvaWuseInfoDao;

@Service
public class EntityEvaWuseInfoService {

	@Autowired
	private EntityEvaWuseInfoDao entityEvaWuseInfoDao;
	
	/**
	 * 插入數值
	 * @param list 真实數值
	 * @param listColumn 列名
	 * @return succss and failed message
	 */
	public Map<String, Object> insertEntityEvaWuseInfo(List<Object> list, List<String> listColumn) {
		Map<String, Object> insertMap = new HashMap<>();
		Map<String, Object> modelMap = new HashMap<>();
		

		//固定字段
		String strSum = "YEAR";
		for(String str : listColumn)
			strSum += ',' + str;
		strSum += ','+"company_id"+','+"user_id";
//		strSum += ",UNIT_PRODUCT_WATER_WITHDRAWAL,REPEAT_UTILIZATION,DCOOL_WATER_CIRCULATION_RATE,ICOOL_WATER_CIRCULATION_RATE"
//				+ ",STEAM_CONDENSATE_REUSE_RATE,WASTE_WATER_REUSE_RATE,LOSS_RATE,COMPLIANCE_RATE,UNCON_WATER_RSRC_REPLAC_RATE";//變化字段在固定字段中間
		insertMap.put("lineColumn", strSum);
		insertMap.put("lineList", list);
		try {
			entityEvaWuseInfoDao.insertEntityEvaWuseInfo(insertMap);
			modelMap.put("status", "success");

		}catch(DuplicateKeyException e) {
			if(e.getMessage().contains("YEAR")) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "年份重复，请重新输入");
				return modelMap;
			}
		}
		return modelMap;
	}
	
	/**
	 * 刪除數據
	 * @param listId delete row id
	 * @return 1 成功；0 失败
	 */
	public int deleteEntityEvaWuseInfo(List<Integer> listId) {
		try {
			//使用增强for循环进行删除
			for(int id : listId) {
				entityEvaWuseInfoDao.deleteEntityEvaWuseInfo(id);
			}
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}
	
	/**
	 * 根據label_id展示評估數據
	 * @param label_id
	 * @return database message
	 */
	public List<Map<String, Object>> selectEntityEvaWuseInfo(int label_id) {
		return entityEvaWuseInfoDao.selectEntityEvaWuseInfo(label_id);
	}
		
	/**
	 * 根據label_id展示評估數據
	 * @param year
	 * @return database message
	 */
	public Boolean selectEntityEvaWuseInfoByYear(int year) {
		return (entityEvaWuseInfoDao.selectEntityEvaWuseInfoByYear(year).isEmpty()? false : true);
	}
	/**
	 * 更新記錄
	 * @param id 記錄id
	 * @param list 記錄數據
	 * @param listColumn 記錄列名
	 * @return 成功返回1，失败返回0
	 */
	public Map<String, Object> updateEntityEvaWuseInfo(int id, List<Object> list, List<String> listColumn) {
		Map<String, Object> modelMap = new HashMap<>();
		int length = listColumn.size();
		//可變字段在固定字段中間
		String strSum = "YEAR=" + "'" + list.remove(0) + "',";
				
		for(int i = 0; i < length; i++) {
			strSum += listColumn.get(i) + "='" + list.remove(0) + "',";
 		}
//		strSum += "UNIT_PRODUCT_WATER_WITHDRAWAL=" + "'" + list.remove(0) + "'," + 
//				"REPEAT_UTILIZATION=" + "'" + list.remove(0) + "',"+
//				"DCOOL_WATER_CIRCULATION_RATE=" + "'" + list.remove(0) + "',"+
//				"ICOOL_WATER_CIRCULATION_RATE=" + "'" + list.remove(0) + "',"+
//				"STEAM_CONDENSATE_REUSE_RATE=" + "'" + list.remove(0) + "',"+
//				"WASTE_WATER_REUSE_RATE=" + "'" + list.remove(0) + "',"+
//				"LOSS_RATE=" + "'" + list.remove(0) + "',"+
//				"COMPLIANCE_RATE=" + "'" + list.remove(0) + "',"+
//				"UNCON_WATER_RSRC_REPLAC_RATE=" + "'" + list.remove(0) + "',";
		strSum = strSum.substring(0, strSum.length() - 1);
		Map<String, Object> sqlMap = new HashMap<>();
		sqlMap.put("id", id);
		sqlMap.put("sql", strSum);
		try {
			entityEvaWuseInfoDao.updateEntityEvaWuseInfo(sqlMap);
			modelMap.put("status", "success");
		}
		catch (DuplicateKeyException e){
        	if(e.getMessage().contains("YEAR")) {
        		modelMap.put("status", "failed");
        		modelMap.put("msg", "年份已存在，请重新输入");
        		return modelMap;
        	}
		}
		return  modelMap;
	}
	
}
